import { Packet } from 'packet-model.js';
var packet = new Packet();
Page({

  data: {
    onLoading:true,
  },

  
  onLoad: function (options) {
   this.data.code = options.code ? options.code : '';
   this.data.id = options.id ? options.id : '';
    if (!this.data.code || !this.data.id){
      wx.showToast({
        title: '参数有误',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    this.receive();
  },
  //领红包
  receive: function () {
    var that = this;
    var args = {
      id: that.data.id,
      encryptCode: that.data.code
    };
    packet.receive(args, (data) => {
      if (data.data.info){
        data.data.info = JSON.parse(data.data.info);
      }
      that.setData({
        item: data.data,
        onLoading: false,
      })
      //领取结果
      if (data.status == 1)
      {
        that.setData({
          success : true
        })
        wx.showToast({
          title: data.msg,
          icon: 'success',
          duration: 2000
        })
       
      }else{
        that.setData({
          success: false
        })
        wx.showToast({
          title: data.msg,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  //去到钱包页面
  toAccount:function(){
    wx.navigateTo({
      url: '../center/wallet',
    })
  },
  //自定义底部跳转
  footRedirect: function (e) {
    var url = e.currentTarget.dataset.url;
    wx.switchTab({
      url: url,
    })
  }
})